<template>
  <div>
    <el-dialog
      width="40%"
      :visible.sync="visible"
      title="修改密码"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="账号" label-width="100px">
          {{ form.username }}
        </el-form-item>
        <el-form-item label="原密码" label-width="100px" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="新密码" label-width="100px" prop="newPassword">
          <el-input v-model="form.newPassword"></el-input>
        </el-form-item>
        <!-- <el-form-item
          label="确认新密码"
          label-width="100px"
          prop="reNewPassword"
        >
          <el-input
            v-model="form.reNewPassword"
            @keyup.enter.native="onSubmit"
          ></el-input>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" @click="onSubmit">修改</el-button>
          <el-button @click="updateUsereClosed">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>
import * as api from "@/api/user.js";
export default {
  NewPd: "",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        this.NewPd = value;
        callback();
      }
    };
    const validateReNewPassword = (rule, value, callback) => {
      if (value != this.NewPd) {
        callback(new Error("两次密码不相同"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      form: {
        username: "",
        password: "",
        newPassword: "",
        reNewPassword: "",
      },
      rules: {
        password: [
          { required: true, message: "请输入原密码", trigger: "blur" },
        ],

        newPassword: [
          { required: true, message: "请输入新的密码", trigger: "blur" },
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        // reNewPassword: [
        //   { required: true, message: "请再次输入新的密码", trigger: "blur" },
        //   { required: true, trigger: "blur", validator: validateReNewPassword },
        // ],
      },
    };
  },
  methods: {
    async logout() {
      await this.$store.dispatch("user/logout");
      this.$router.push(`/login?redirect=${this.$route.fullPath}`);
      location.reload();
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          api
            .changePass({
              username: this.form.username,
              rawPass: this.form.password,
              newPass: this.form.newPassword,
            })
            .then((response) => {
              this.$message.success(response.msg + "，请重新登录");

              // this.updateUsereClosed();
              this.logout();
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    updateUserClosed() {
      this.$emit("collBackCourse");
    },
    updateUsereClosed() {
      this.form.password = "";
      this.form.newPassword = "";
      this.form.reNewPassword = "";
      this.visible = false;
    },
    open(val) {
      this.form = {};
      this.visible = true;
      this.form.username = val;
    },
  },
};
</script>
